<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="srcPath" value="${redis:getSysConfig('sysFileVisitRoot_back')}"/>
<script type="text/javascript" src="${basePath}/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="${basePath}/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="${basePath}/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="${srcPath}/common/js/common.js"></script>

<script type="text/javascript">
    /**
     * 自定义验证规则
     */
    function validateForm(){
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                paramName: {
                    validators: {
                        notEmpty: {message: "参数名称不可为空"},
                        stringLength: {max: 50,message: "参数名称最大长度为50"},
                        threshold :  1 ,
                        /**bootstrap validator 远程校验需要返回{"valid",true or false}**/
                        remote: {
                            url:'${basePath}/back/productParameter/checkName.sjson',
                            message:  '参数已存在',
                            delay:  2000,
                            type:  'POST',
                            data: function(validator) {
                                return {
                                    paramName:$("#paramName").val(),
                                    productTypeId:$("#productTypeId").val(),
                                    paramId:$("#paramId").val()
                                };
                            }
                        }
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {message: "排序号不可为空"},
                        stringLength: {max: 4,message: "排序号最大长度为4"},
                        numeric: {
                            message: '排序号只能输入数字'
                        }
                    }
                }

            }
        });
    }

    var itemIndex = 0;/**初始化动态添加属性值的下标值**/
    /**
     * 添加参数信息
     */
	<shiro:hasPermission name="back_gotoProductParameterPage_add">
    function addInfo(){
        $("#myModalLabel").empty().text("添加参数信息");
        /**清空隐藏域的值**/
        var productTypeId = $("#q_productTypeId").val();
		getProductTypeName(productTypeId);
        $(".addParamValue").remove();////添加参数值
        $("#paramId").val("");
        $("#productTypeId").val(productTypeId);
        itemIndex = 0;
        /**清空表单控件中值**/
        resetAddForm();
    }
	</shiro:hasPermission>

    function getProductTypeName(id){
        $.ajax({
            type: "POST",
            dataType: "JSON",
            url: "${basePath}/back/productParameter/getProductParameterInfo.sjson",
            data: {productTypeId:id},
            success: function(result){
                var data = JSON.parse(result.data);
                var shopProductType =data.shopProductType;
                $("#productTypeName").html(shopProductType.sortName);
            }
        });
    }

    /**
     * 编辑参数信息
     */
	<shiro:hasPermission name="back_gotoProductParameterPage_update">
    function editInfo(){
        $("#myModalLabel").empty().text("修改参数信息");
        /**获取选中行的id**/
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var id=selectedRow[0].paramId;
            $("#btn_edit").attr("data-target","#addOrEditModal");/**选中了记录后才可打开对应的模态框**/
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "${basePath}/back/productParameter/getProductParameterInfo.sjson",
                data: {paramId:id},
                success: function(result){
                    var data = JSON.parse(result.data);
                    itemIndex = 0;//将动态添加属性值的下标值置0
                    var shopProductType =data.shopProductType;
                    $("#productTypeName").html(shopProductType.sortName);
                    var shopProductParameter =data.shopProductParameter;
                    $("#paramName").val(shopProductParameter.paramName);
                    $("#sort").val(shopProductParameter.sort);
                    $("#productTypeId").val(shopProductParameter.productTypeId);
                    $("#paramId").val(shopProductParameter.paramId);
                    initParamValue(data.shopProductParameterValueList);
                }
            });
        }else{
            $("#btn_edit").attr("data-target","");/**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
	</shiro:hasPermission>

    function addParamValue(){
        var html = "<tr class='addParamValue'>"+
            "<td align='center'><div class='form-group'><input id='sortValue"+itemIndex+"' type='text' name='shopProductParameterValueList["+itemIndex+"].sort' class='form-control input-sm {validate:{required:true,digits:true}} paramValueSort' style='width:50px;' value='"+getMaxSortNum('paramValueSort')+"'/></div></td>"+
            "<td colspan='2' align='center'><div class='form-group'><input id='paramValueName"+itemIndex+"' type='text' name='shopProductParameterValueList["+itemIndex+"].paramValueName' class='form-control input-sm {validate:{required:true}}' value=''/></div></td>"+
            "<td align='center'><a onclick='remove_tr($(this),null);' href='javascript:'>移除</a></td>"+
            "</tr>";
        $('#tr_model > tr:last').after(html);
        itemIndex++;
    }

    /**移除文本**/
    function remove_tr(o,paramValueId){
        if(paramValueId!=null){
            $.ajax({
                type: "POST",dataType: "JSON",
                url: "${basePath}/back/productParameter/deleteShopProductParameterValue.sjson",
                data: {paramValueId:paramValueId},
                success: function(data){
                    if(data.success==true){
                        o.parents('tr:first').remove();
                        showMsg("移除成功！");
                    }else{
                        showMsg("移除失败！");
                    }
                }
            });
        }else{
            o.parents('tr:first').remove();
        }
    }
    /**初始化参数值**/
    function initParamValue(shopProductParameterValueList){
        $(".addParamValue").remove();
        if(shopProductParameterValueList!=null){
            for(var i=0;i<shopProductParameterValueList.length;i++){
                var html = "<tr class='addParamValue'>"+
                    "<td align='center'><div class='form-group'><input id='sortValue"+itemIndex+"' type='text' name='shopProductParameterValueList["+itemIndex+"].sort' class='form-control input-sm {validate:{required:true,digits:true}} paramValueSort' value='"+shopProductParameterValueList[i].sort+"' style='width:50px;'/></div></td>"+
                    "<td  colspan='2' align='center'><input id='paramValueId"+itemIndex+"' name='shopProductParameterValueList["+itemIndex+"].paramValueId' type='hidden' value='"+shopProductParameterValueList[i].paramValueId+"'/><div class='form-group'><input class='form-control input-sm {validate:{required:true}}' id='nameValue"+itemIndex+"' type='text' name='shopProductParameterValueList["+itemIndex+"].paramValueName' value='"+shopProductParameterValueList[i].paramValueName+"'/></div></td>"+
                    "<td align='center'><a onclick='remove_tr($(this),"+shopProductParameterValueList[i].paramValueId+");' href='javascript:'>移除</a></td>"+
                    "</tr>";
                $('#tr_model > tr:last').after(html);
                itemIndex++;
            }
        }
    }

</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1"  method="post" action="${basePath}/back/productParameter/saveOrUpdateProductParameter.sjson">
					<input id="productTypeId" type="hidden" name="productTypeId" noclear="true" />
					<input id="paramId" type="hidden" name="paramId" />

					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">

						<tr>
							<td class="titlebg"colspan="4" style="font-weight:bold;text-align: center;">属性信息</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span> 所属分类:&nbsp;&nbsp;</td>
							<td  class="toleft_td" colspan="3">
								<div class="form-group">
									<span id="productTypeName"></span>
								</div>
							</td>
						</tr>

						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span> 参数名称:&nbsp;&nbsp;</td>
							<td  class="toleft_td" colspan="3">
								<div class="form-group">
									<input id="paramName" class="form-control input-sm" type="text" name="paramName" placeholder="请输入参数名称"/>
								</div>
							</td>
						</tr>

						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span> 排序号:&nbsp;&nbsp;</td>
							<td  class="toleft_td" colspan="3">
								<div class="form-group">
									<input id="sort" class="form-control input-sm" type="text" name="sort" placeholder="请输入排序号"/>
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="4" class="titlebg" style="font-weight:bold;text-align: center;">添加参数值</td>
						</tr>
						<tr class="titlebg">
							<td  width="20%"  style="font-weight:bold;text-align: center;"><span style="color:red">* </span>排序号</td>
							<td  width="40%" colspan="2"  style="font-weight:bold;text-align: center;"><span style="color:red">* </span>参数值</td>
							<td  width="30%"  style="font-weight:bold;text-align: center;"><a class="btn-add marginleft" id="add_type" href="javascript:addParamValue();"> <span>添加参数值</span> </a></td>
						</tr>
						<tbody id="tr_model">
						<tr id="0"></tr>
						</tbody>

					</table>

				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary"  onclick="submitFormByRemote()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>

		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>